package demo.web;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.time.Duration;
import java.time.LocalDateTime;


@RestController
public class SseController {

    @GetMapping(value = "/sse/time/{cnt}", produces = "text/event-stream")
    public Flux<String> stream(@PathVariable int cnt) {
        return Flux.range(1, cnt).delayElements(Duration.ofSeconds(1))
                .map(sequence -> {
                    if (sequence < 10) {
                        return sequence + " Current time: " + LocalDateTime.now();
                    }
                    throw new RuntimeException("Error occurred");
                });
    }
}
